﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading;
using System.DirectoryServices;
using System.Collections;

using groupAnalyzer.Observers;
using groupAnalyzer.model;
using groupAnalyzer;
using groupAnalyzer.connectivity;

namespace groupAnalyzer.controller
{
    public class SettingsController : ObserverClient
    {
        private volatile Messages message;
        private volatile Thread myThread;
        private object payLoad;
        private Observer observer;

        public void notify(Messages message, object payLoad)
        {
            this.message = message;
            this.payLoad = payLoad;
            // wake worker
            if (this.myThread.ThreadState == ThreadState.Suspended)
                this.myThread.Resume();
        }

        public void run()
        {
            while (true)
            {
                if (this.message == Messages.nothing)
                {
                    // be nice to cpu
                    this.myThread.Suspend();
                }
                switch (this.message)
                {
                    case Messages.saveSettings:
                        Settings settings = Settings.getSettings();
                        settings.saveSettings();
                        break;

                    case Messages.loadSettings:
                        Settings.loadSettings();
                        break;

                    default:
                        break;
                }
                // nothing to do any more - set it to nothing
                this.message = Messages.nothing;

            }
        }

        public SettingsController(Observer observer)
        {
            this.observer = observer;
            this.myThread = new Thread(new ThreadStart(this.run));

            myThread.Start();
        }

        public static void init(Observer observer)
        {
            SettingsController settingsController = new SettingsController(observer);
            observer.register(settingsController);
        }
    }
}
